package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class User implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long id;
	private String firstName;
	private String lastName;
	private String email;
	private String phone;
	private List<String> permissions;
	
	/**
	 * create empty User object
	 */
	public User() {
		this.id = null;
		this.firstName = null;
		this.lastName = null;
		this.email = null;
		this.phone = null;
		this.permissions = null;
	}
	
	/**
	 * Creates user object with all parameters
	 * @param id User's id
	 * @param firstName User's first name
	 * @param lastName User's last name
	 * @param email User's email
	 * @param phone User's phone number
	 * @param permissions User's permissions in the system
	 */
	public User( Long id, String firstName, String lastName, String email, String phone, String permissions) {
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
		this.phone = phone;
		String[] perms = permissions.split(",");
		this.permissions = new ArrayList<String>();
		for (String perm : perms) {
			this.permissions.add(perm);
		}
	}
	
	/**
	 * Creates user object with all parameters
	 * @param id User's id
	 * @param firstName User's first name
	 * @param lastName User's last name
	 * @param email User's email
	 * @param phone User's phone number
	 */
	public User( Long id, String firstName, String lastName, String email, String phone) {
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
		this.phone = phone;
		this.permissions = null;
	}

	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}
	
	public void setFirstName(String firstName) {
		if(firstName == null)
			this.firstName = "";
		else 
			this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}
	
	public void setLastName(String lastName) {
		if(lastName == null)
			this.lastName = "";
		else
			this.lastName = lastName;
	}
	
	public String getEmail() {
		return email;
	}
	
	public void setEmail(String email) {
		if(email == null) {
			this.email = "";
		}
		else
			this.email = email;
	}

	public String getPhone() {
		return phone;
	}
	
	public void setPhone(String phone) {
		if(phone == null) 
			this.phone = "";
		else
			this.phone = phone;
	}

	public List<String> getPermissions() {
		return permissions;
	}
	
	public void setPermissions(List<String> permissions) {
		this.permissions = permissions;
	}
	
	public boolean hasPermission(String perm) {
		return this.permissions.contains(perm);
	}
}
